package cn.kduck.plugin.idea;

import cn.kduck.plugin.idea.utils.StringUtils;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileNameConvertAction extends AnAction {

    private static final Pattern FIELD_NAME_PATTERN = Pattern.compile("^[A-Z][A-Z0-9_]*$");

    private String[] KEY_WORDS = {"private","protected","public","static","final","String","Integer","Date","Long","Boolean","Double","int","long","boolean","double","char","true","false","for","if","else","while","switch","case","continue","break","class","abstract","extends","interface","enum","import","return"};

    @Override
    public void actionPerformed(AnActionEvent e) {
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        Project project = e.getData(CommonDataKeys.PROJECT);

        if (editor != null) {
            SelectionModel selectionModel = editor.getSelectionModel();
            String selectedText = selectionModel.getSelectedText();
            if (selectedText != null) {
                String transformedText = transformText(selectedText);

                WriteCommandAction.runWriteCommandAction(project,()->{
                    editor.getDocument().replaceString(
                            selectionModel.getSelectionStart(),
                            selectionModel.getSelectionEnd(),
                            transformedText
                    );
                });
            }
        }
    }

    private String transformText(String selectedText){
        String[] words = selectedText.split(" ");
        Map<String,String> replaceFieldMap = new HashMap<>();
        for (String word : words) {
            String wordTrim = word.trim();
            if(!"".equals(wordTrim)){
                int index = wordTrim.indexOf(";");
                boolean endWithSemicolon = index != -1;
                if(endWithSemicolon){
                    wordTrim = wordTrim.substring(0,index);
                }

                if(isKeyWord(wordTrim)){
                    continue;
                }

                Matcher matcher = FIELD_NAME_PATTERN.matcher(wordTrim);
                String convertedWord;
                if(matcher.matches()){
                    convertedWord = StringUtils.clearSeparator(wordTrim,'_');
                }else{
                    convertedWord = StringUtils.upperCaseWithSeparator(wordTrim,"_");
                }
                replaceFieldMap.put(wordTrim,convertedWord);
            }
        }
        for (Entry<String, String> entry : replaceFieldMap.entrySet()) {
            selectedText = selectedText.replaceAll(entry.getKey(),entry.getValue());
        }
        return selectedText;
    }

    private boolean isKeyWord(String word){
        for (String keyWord : KEY_WORDS) {
            if(keyWord.equals(word)){
                return true;
            }
        }
        return false;
    }
}
